const Base = require('./base.js');

// module.exports = class extends Base {
module.exports = class extends  think.Controller {
	constructor(...arg) {
		super(...arg);
	}
	openAction() {
		let ws = this.websocket,
			data = ws.handshake.query,
			name = data.name,
			room = data.room;
		think.logger.debug('>> websocket: ' + name + ' 链接成功 <<')
		ws.emit('opend',name + ' 连接成功');
		ws.join(room,function () {
			ws.in(room).emit('message',name + ' 上线');
			setInterval(function(){
				ws.emit('message',(80 + Math.floor(Math.random()*10)),think.datetime(new Date(),'HH:mm:ss'));
			},500)
		})
		return this.success();
	}
	messageAction() {
		let data = this.wsData,
			ws = this.websocket;
		think.logger.debug('>> 收到消息： ' + data + ' <<')
		// ws.emit('message','消息-'+data+think.datetime());
		return this.success();
	}

	closeAction() {
		let ws = this.websocket,
			data = ws.handshake.query,
			name = data.name,
			room = data.room;
		ws.leave(room,function () {
			ws.to(room).emit('message',name + '下线');
		})
		think.logger.debug('>> websocket: ' + name + ' 已关闭 <<')
		return this.success();
	}

};
